﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RealmMudLib.GameObjects.Base;
using RealmMudLib.Screens.Base;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace RealmMudLib.GameObjects
{
    /// <summary>
    /// A basic button
    /// </summary>
    public class Button : GameObject
    {
        private Texture2D Normal { get; set; }
        private Texture2D Selected { get; set; }
        private Texture2D Current { get; set; }
        private string NormalText { get; set; }
        private string SelectedText { get; set; }
        private string CurrentText { get; set; }
        private SpriteFont Font { get; set; }
        private Rectangle Bounds { get; set; }
        private bool IsClicked = false;

        public Button(Vector2 location, Texture2D normalGraphic, string normalText, 
            Texture2D selectedGraphic, string selectedText, 
            GameScreen owner)
            : base(owner)
        {
            this.Normal = normalGraphic;
            this.Current = this.Normal;
            this.Bounds = new Rectangle((int)location.X, (int)location.Y, this.Current.Width, this.Current.Height);
            this.NormalText = normalText;
            this.CurrentText = this.NormalText;
            this.Selected = selectedGraphic;
            this.SelectedText = selectedText;
        }
        public override void LoadContent()
        {
            this.Font = this.ParentScreen.Manager.BaseGame.Content.Load<SpriteFont>("Fonts\\Arial");
            base.LoadContent();
        }
        public override void Update(Microsoft.Xna.Framework.GameTime gameTime)
        {
            // Checking our input
            if (this.ParentScreen.Manager.Input.Rectangle.Intersects(this.Bounds))
            {
                if (!IsClicked)
                {
                    this.Current = this.Selected;
                    this.CurrentText = this.SelectedText;
                }
                // See if we are clicked
                if (this.ParentScreen.Manager.Input.NewLeftClick)
                {
                    if (!IsClicked)
                    {
                        this.CurrentText = "Clicked!!";
                        IsClicked = true;
                    }
                    else { IsClicked = false; }
                }
                

            }
            else
            {
                if (!IsClicked)
                {
                    this.Current = this.Normal;
                    this.CurrentText = this.NormalText;
                }
            }
            base.Update(gameTime);
        }
        public override void Draw(Microsoft.Xna.Framework.GameTime gameTime)
        {
            this.ParentScreen.spriteBatch.Begin();
            this.ParentScreen.spriteBatch.Draw(this.Current, this.Bounds, Color.Blue);
            this.DrawString(this.ParentScreen.spriteBatch, this.Font, this.CurrentText, this.Bounds);
            this.ParentScreen.spriteBatch.End();
            base.Draw(gameTime);
        }
    }
}
